<?php
namespace app\adm\controller;

class Setting extends Base
{
	// 基本设置
	public function index()
	{
		if (is_post()) {
			$post = input('post.');
			foreach ($post as $k => $v) {
				db('config')->where('key', $k)->setField('value', $v);
			}
			cache('sys_config',null);
			$this->success('设置成功');
		}
		return $this->fetch();
	}

	// 收益设置
	public function award()
	{
		if (is_post()) {
			$post = input('post.');
			foreach ($post as $k => $v) {
				db('config')->where('key', $k)->setField('value', $v);
			}
			cache('sys_config',null);
			$this->success('设置成功');
		}
		return $this->fetch();
	}
	
	// 转账设置
	public function trans()
	{
		if (is_post()) {
			$post = input('post.');
			$rst = db('user_coin_trans')->where('id', $post['id'])->update($post);
			$this->success('设置成功');
		}

		if (input('get.id')) {
			return db('user_coin_trans')->where('id', input('get.id'))->find();
		}

		$list = db('user_coin_trans')->paginate();
		$this->assign('list', $list);
		return $this->fetch();
	}
	

	// 变更账户信息
	public function updateAccount()
	{
		if (is_post()) {
			$post = input('post.');
			foreach ($post as$v) {
				if (empty($v)||(strlen($v)<4)) {
					$this->error('请输入有效值');
				}
			}
			if (input('post.new_pwd') == input('post.rnew_pwd')) {
				$sele = array();
				$sele['id'] = session('aid');
				$sele['ad_tel'] = input('post.ad_tel');
				$rst = db('admin')->where($sele)->find();
				if ($rst && (encrypt(input('post.ad_pwd')) == $rst['ad_pwd'])) {
					$post['ad_pwd'] = encrypt($post['new_pwd']);
					unset($post['new_pwd']);
					unset($post['rnew_pwd']);
					$row = db('admin')->where('id',$rst['id'])->update($post);
					if ($row) {
						$this->success('修改生效，下次登录生效');
					}
					$this->error('请稍后重试');
				}
				$this->error('账号或原始密码错误');
			}
			$this->error('新密码两次输入不一致');
		}
		$info = db('admin')->where('id', session('aid'))->find();
		$this->assign('info', $info);
		return $this->fetch();
	}

	public function receipt ()
	{
		if (is_post()) {
			$post = input('post.');
			if (db('receipt')->where('id',1)->update($post))
				$this->success('修改成功');
			$this->error("稍后重试");
		}
		$receipt = db('receipt')->where('id',1)->find();
		$this->assign('receipt',$receipt);
		return $this->fetch();
	}

	//上传图片
	public function upload() {
		$file = request()->file('file');
		$info = $file->validate(['size' => '4096000'])
			->move(ROOT_PATH . 'public/uploads/');
		if ($info) {
			$path = '/uploads/' . $info->getsavename();
			$path = str_replace('\\', '/', $path);
			return $data = array(
				'code' => 1,
				'msg' => '上传成功',
				'data' => $path,
			);
		} else {
			return $data = array(
				'msg' => $file->getError(),
				'code' => 0,
			);
		}
	}
}